package com.toyi.util;

import java.util.StringTokenizer;

/**
 * Singleton class.
 * This class contains string-related utilities such as string tokenizing and parsing.
 * Methods provided here are commonly-used cases and are made as generic as possible
 * such that it can be used by most applications.
 */
public class TokenUtil
{
	//---------------------------------------------------------------------------------------------
	// Constructors
	//---------------------------------------------------------------------------------------------
	/**
	 * Empty constructor.
	 */
	private TokenUtil()
	{
	}

	//---------------------------------------------------------------------------------------------
	// Class Methods
	//---------------------------------------------------------------------------------------------
	/**
	 * Gets a particular field in a delimited string.
	 * @param parseString the string to be parsed.
	 * @param delimiter the character that separates the string into tokens.
	 * @param field the field that is required.
	 * @return Returns the required field.
	 */
	public static String getField(String parseString, char delimiter, int field)
	{
		if (parseString == null || parseString.trim().length() == 0) return null;

		SimpleStringTokenizer st = new SimpleStringTokenizer(parseString, delimiter);
		String name = null;

		// Strip out the field
		for (int i = 0; i < field; i++)
		{
			if (st.hasMoreTokens())
				name = st.nextToken();
			else
				return null;
		}

		return name;
	}

	/**
	 * Gets a particular field in a delimited string.
	 * @param parseString the string to be parsed.
	 * @param delimiter the characters that separates the string into tokens.
	 * @param field the field that is required.
	 * @return Returns the required field.
	 */
	public static String getField(String parseString, String delimiter, int field)
	{
		if (parseString == null || delimiter == null ||
				parseString.length() == 0 || delimiter.length() == 0)
			return null;
		if (delimiter.length() == 1)
			return getField(parseString, delimiter.charAt(0), field);

		StringTokenizer st = new StringTokenizer(parseString, delimiter);
		String name = null;

		// Strip out the field
		for (int i = 0; i < field; i++)
		{
			if (st.hasMoreTokens())
				name = st.nextToken();
			else
				return null;
		}

		return name;
	}

	/**
	 * Finds out whether a string is equal to a field in a delimited string.
	 * @param parseString the string to be parsed.
	 * @param delimiter the characters that separates the string into tokens.
	 * @param findString the string to compare.
	 * @return True if the strings are matched, false otherwise.
	 */
	public static boolean isExists(String parseString, char delimiter, String findString)
	{
		if (parseString == null || parseString.length() == 0) return false;

		SimpleStringTokenizer st = new SimpleStringTokenizer(parseString, delimiter);
		while (st.hasMoreTokens())
		{
			if (findString.equals(st.nextToken()))
				return true;
		}

		return false;
	}

	/**
	 * Finds out whether a string is equal to a field in a delimited string.
	 * @param parseString the string to be parsed.
	 * @param delimiter the characters that separates the string into tokens.
	 * @param findString the string to compare.
	 * @return True if the strings are matched, false otherwise.
	 */
	public static boolean isExists(String parseString, String delimiter, String findString)
	{
		if (parseString == null || delimiter == null ||
				parseString.length() == 0 || delimiter.length() == 0)
			return false;
		if (delimiter.length() == 1)
			return isExists(parseString, delimiter.charAt(0), findString);

		StringTokenizer st = new StringTokenizer(parseString, delimiter);
		while (st.hasMoreTokens())
		{
			if (findString.equals(st.nextToken()))
				return true;
		}

		return false;
	}

/*
	public static void main(String[] args)
	{
		String s = "apple.orange.banana.grapes";
		char delimiter = '.';

		System.out.println("Parsing: "+s+" with delimiter: "+delimiter);
		for (int i=0; i < 6; i++)
			System.out.println("field:"+i+" is "+getField(s, delimiter, i));

		System.out.println("=============================================================");

		delimiter = ':';
		System.out.println("Parsing: "+s+" with delimiter: "+delimiter);
		for (int i=0; i < 6; i++)
			System.out.println("field:"+i+" is "+getField(s, delimiter, i));

		System.out.println("=============================================================");

		delimiter = '.';
		System.out.println("String: "+s+"  Fields delimiter: "+delimiter);
		System.out.println("apple exists?: "+isExists(s, delimiter, "apple"));
		System.out.println("banana exists?: "+isExists(s, delimiter, "banana"));
		System.out.println("grapes exists?: "+isExists(s, delimiter, "grapes"));
		System.out.println("mango exists?: "+isExists(s, delimiter, "mango"));

		System.out.println("=============================================================");

		delimiter = '=';
		System.out.println("String: "+s+"  Fields delimiter: "+delimiter);
		System.out.println("apple exists?: "+isExists(s, delimiter, "apple"));
		System.out.println("banana exists?: "+isExists(s, delimiter, "banana"));
		System.out.println("grapes exists?: "+isExists(s, delimiter, "grapes"));
		System.out.println("mango exists?: "+isExists(s, delimiter, "mango"));
		System.out.println();

		delimiter = '.';
		long start = System.currentTimeMillis();
		for (int i=0; i < 1000000; i++)
		{
			for (int j=0; j < 6; j++)
				getField(s, delimiter, j);
		}
		long stop = System.currentTimeMillis();
		System.out.println("char delimiter takes "+(stop-start)+" ms");

		String delimiterStr = ".";
		start = System.currentTimeMillis();
		for (int i=0; i < 1000000; i++)
		{
			for (int j=0; j < 6; j++)
				getField(s, delimiterStr, j);
		}
		stop = System.currentTimeMillis();
		System.out.println("string delimiter takes "+(stop-start)+" ms");
	}
*/
}
